package com.example.xiongmao.controller;

import com.example.xiongmao.constant.JwtConstant;
import com.example.xiongmao.constant.RoleConstant;
import com.example.xiongmao.entity.LoginDTO;
import com.example.xiongmao.entity.Result;
import com.example.xiongmao.entity.ShopManager;
import com.example.xiongmao.entity.VO.admin.adminLoginVO;
import com.example.xiongmao.entity.Admin;
import com.example.xiongmao.exception.AccountNotFoundException;
import com.example.xiongmao.properties.JwtProperties;
import com.example.xiongmao.service.ShopManagerChargeService;
import com.example.xiongmao.service.adminChargeService;
import com.example.xiongmao.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class loginController {

    @Autowired
    private adminChargeService adminchargeService;

    @Autowired
    private ShopManagerChargeService shopManagerService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<adminLoginVO> login(@RequestBody LoginDTO LoginDTO) {
        System.out.println("员工登录：" + LoginDTO);
        System.out.println("当前登录的角色是："+LoginDTO.getRole());
        Admin admin = null;
        ShopManager shopmanager = null;
        // 根据角色选择不同的服务进行登录
        if (RoleConstant.ADMIN.equals(LoginDTO.getRole())) {
            admin = adminchargeService.login(LoginDTO);
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtConstant.ADMIN_ID, admin.getId());
            claims.put(JwtConstant.ADMIN_ROLE,admin.getRole());
            String token = JwtUtils.createJwt(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

            adminLoginVO adminloginVO = adminLoginVO.builder()
                    .id(admin.getId())
                    .username(admin.getUsername())
                    .realName(admin.getRealName())
                    .role(admin.getRole())   // 该role是判断登录之后的角色
                    .token(token)
                    .build();
            return Result.success(adminloginVO);
        } else if (RoleConstant.USER.equals(LoginDTO.getRole())) {
            shopmanager = shopManagerService.login(LoginDTO);
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtConstant.SHOP_MANAGER_ID, shopmanager.getId());
            claims.put(JwtConstant.SHOP_MANAGER_ROLE,shopmanager.getRole());
            String token = JwtUtils.createJwt(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

            adminLoginVO adminloginVO = adminLoginVO.builder()
                    .id(shopmanager.getId())
                    .username(shopmanager.getUsername())
                    .realName(shopmanager.getRealName())
                    .role(shopmanager.getRole())   // 该role是判断登录之后的角色
                    .token(token)
                    .build();
            return Result.success(adminloginVO);
        } else {
            throw new AccountNotFoundException("无效的角色类型");
        }
        // 先查询  账号密码是否对头
//        Admin admins = adminchargeService.login(LoginDTO);

    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin adminregistDTO){
        System.out.println("正在注册："+adminregistDTO);


        adminchargeService.addRegister(adminregistDTO);
        return Result.success();
    }
}
